<?php 

class UploadController extends BaseController {

	// Table
	public $table = 'upload';

	// Eloquent Model
	public $model = 'Upload';

	// Base Route
	public $base = 'upload';

	// Localization
	public $local = 'en';

	// Language Name
	public $language = 'upload';

	// Breadcrumbs
	public $breadcrumbs = [ ['link' => 'upload' , 'title' => 'Upload'] ];
	
	// Set List Data - Input, Object
	public function setListData( $data, $object ) {
		$object = $object->select(DB::raw('user.username, upload.*'));
		$object = $object->join('user', 'upload.added_by', '=', 'user.id');

		if( Input::get('filter_username') ) 
			$object = $object->whereRaw('LCASE(`username`) LIKE LCASE("%' . Input::get('filter_username') . '%")');

		if( Input::get('filter_filename') ) 
			$object = $object->whereRaw('LCASE(`filename`) LIKE LCASE("%' . Input::get('filter_filename') . '%")');		
		
		if( Input::get('filter_uploaded_from') && !Input::get('filter_uploaded_to') )
			$object = $object->whereRaw('DATE(upload.created_at)  = "' . Formatter::toDateFormat(Input::get('filter_uploaded_from')) . '"' );

		if( !Input::get('filter_uploaded_from') && Input::get('filter_uploaded_to') )
			$object = $object->whereRaw('DATE(upload.created_at)  = "' . Formatter::toDateFormat(Input::get('filter_uploaded_to')) . '"' );

		if( Input::get('filter_uploaded_from') && Input::get('filter_uploaded_to') )
			$object = $object->whereRaw('DATE(upload.created_at)  BETWEEN "' . Formatter::toDateFormat(Input::get('filter_uploaded_from')) . '" AND "' .  Formatter::toDateFormat(Input::get('filter_uploaded_to')) . '"' );

		return $object;
	}

	public function preStore( $data ) {

		// If Upload Type is Empty
		if( empty($data['type']) )
				$this->error['type'][] = Lang::get('upload.error_type');

		// Check if there's a file
		if(!Input::hasfile('original_filename')) {
			$this->error['original_filename'][] = Lang::get('upload.error_file');

		} else {

			// Get File
			$file = Input::file('original_filename');
			
			// Get Original Filename
			$name = $file->getClientOriginalName();

			// Get Size of the file by MB
			$size = round(($file->getSize() / 1024) / 1024, 2);

			// Get Extension of the file
			$extension = $file->getClientOriginalExtension();

			// Set Orginial Filename
			$data['original_filename'] = $name;

			// Set New Filename
			$data['filename'] = $data['type'] . '-' . date("Ymd-his") . $name;

			# FILE VALIDATIONS

			// If Size is Greater than 1 MB
			if( $size > 1 )
				$this->error['original_filename'][] = Lang::get('upload.error_file_size');

			// If file is not CSV
			if( strtolower($extension) != 'csv') 
				$this->error['original_filename'][] = Lang::get('upload.error_file_extension');

			// If file was not uploaded
			$moved = $file->move(Config::get('app.temp'), $data['filename']);

			#if( !$moved ) 
				#$this->error['original_filename'][] = Lang::get('upload.error_file_upload');

			if( !empty($data['type']) && $moved) {

				$filepath = Config::get('app.temp') . $data['filename'];

				$class = studly_case( $data['type'] ) . 'Upload';
				
				$class = new $class;

				$errors = $class->upload( $filepath );

				if( $errors )
					$this->error['original_filename'] = Lang::get('upload.error_file_uploaded');

			}


		}
		return $data;
	}

	public function getUploadFormat() {
		$language = Lang::get('upload.uploads');

		return $language[Input::get('type')]['format'];

	}
}
